//
//  UndetectedWorkListVC.m
//  New_Runway
//
//  Created by mack on 2018/2/12.
//  Copyright © 2018年 NelsonJ. All rights reserved.
//

#import "UndetectedWorkListVC.h"
#import "CheckListModel.h"
#import "CheckListCell.h"
#import "BuyCarDetailVC.h"


@interface UndetectedWorkListVC ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic, strong) UITableView *tableview;
@property (assign, nonatomic) NSInteger pageNo;
@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation UndetectedWorkListVC
-(NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationBarView.titleLabel.text = @"车辆复检";
    
    self.view.backgroundColor = kColorWhite();
    [self initTableview];
    self.pageNo = 1;
    [self loadCheckList];
}
-(void)initTableview{
    kSelfWeak;
    self.tableview = [[UITableView alloc] init];
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    _tableview.estimatedRowHeight = 0;
    _tableview.estimatedSectionHeaderHeight = 0;
    _tableview.estimatedSectionFooterHeight = 0;
    if (@available(iOS 11.0, *)) {
        self.tableview.contentInsetAdjustmentBehavior = UIApplicationBackgroundFetchIntervalNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = false;
    }
    self.tableview.backgroundColor = UIColorHex(0xf5f5f5);
    self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableview registerNib:[UINib nibWithNibName:@"CheckListCell" bundle:nil] forCellReuseIdentifier:@"CheckListCell"];
    self.tableview.mj_header=[MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(downPullRefresh)];
    self.tableview.mj_footer=[MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(upPullLoadMore)];
    [self.view addSubview:self.tableview];
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.view).mas_equalTo(self.navH);
        make.bottom.equalTo(weakSelf.view).mas_equalTo(-self.BotH);
        make.left.right.equalTo(weakSelf.view);
    }];
    
    
    
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 140;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    checkResult *result = self.dataArray[indexPath.row];
    CheckListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CheckListCell"];
    cell.result = result;
//    cell.isCheck = self.isdetected;
    cell.nav = self.navigationController;
    cell.index = indexPath;
    cell.dataArray = self.dataArray;
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    checkResult *result = self.dataArray[indexPath.row];
    BuyCarDetailVC *carDVC = [[BuyCarDetailVC alloc] init];
    carDVC.carID = result.carId;
    [self.navigationController pushViewController:carDVC animated:YES];
    
    
}

- (void)reloadData {
    
    NSString *url = [NSString stringWithFormat:@"%@%@",Api,checkListApi];
    NSDictionary *params = @{
                             @"marketId":[NRWCarUser sharedUser].marketId,
                             @"detectUserId":[NRWCarUser sharedUser].userId,
                             @"pageIndex":@(self.pageNo),
                             @"pageSize":@(15 * self.pageNo)
                             };
    [SVProgressHUD show];
    [YQNetworking postWithUrl:url refreshRequest:YES cache:NO params:params progressBlock:^(int64_t bytesRead, int64_t totalBytes) {
        
    } successBlock:^(id response) {
        CheckListModel *model = [CheckListModel mj_objectWithKeyValues:response];
        if (self.pageNo == 1) {
            [self.dataArray removeAllObjects];
        }
        if (model.code == 1) {
            for (NSDictionary *dic in model.result) {
                checkResult *result = [checkResult mj_objectWithKeyValues:dic];
                [self.dataArray addObject:result];
            }
            
            [self.tableview reloadData];
        }
        if (self.dataArray.count == 0) {
            [self.tableview addDefaultViewWithText:@"暂无数据" defaultImageType:DefaultImageTypeNoData];
        }else{
            [self.tableview removeDefaultView];
        }
        [SVProgressHUD dismiss];
        [self endRefresh];
    } failBlock:^(NSError *error) {
        [ShowMessage showHintMessage:@"请求失败!" duration:2.0 withRemindId:remindFail];
        [self endRefresh];
        [SVProgressHUD dismiss];
        
    }];
}

-(void)downPullRefresh{
    self.pageNo = 1;
    [self loadCheckList];
}
-(void)upPullLoadMore{
    self.pageNo += 1;
    [self loadCheckList];
}
-(void)endRefresh{
    [self.tableview.mj_header endRefreshing];
    [self.tableview.mj_footer endRefreshing];
}
-(void)loadCheckList{
    
    NSString *url = [NSString stringWithFormat:@"%@%@",Api,checkListApi];
    NSDictionary *params = @{
                             @"marketId":[NRWCarUser sharedUser].marketId,
                             @"detectUserId":[NRWCarUser sharedUser].userId,
                             @"pageIndex":@(self.pageNo),
                             @"pageSize":@(15)
                             };
    [SVProgressHUD show];
    [YQNetworking postWithUrl:url refreshRequest:YES cache:NO params:params progressBlock:^(int64_t bytesRead, int64_t totalBytes) {
        
    } successBlock:^(id response) {
        CheckListModel *model = [CheckListModel mj_objectWithKeyValues:response];
        if (self.pageNo == 1) {
            [self.dataArray removeAllObjects];
        }
        if (model.code == 1) {
            for (NSDictionary *dic in model.result) {
                checkResult *result = [checkResult mj_objectWithKeyValues:dic];
                [self.dataArray addObject:result];
            }
                        
            [self.tableview reloadData];
        }
        if (self.dataArray.count == 0) {
            [self.tableview addDefaultViewWithText:@"暂无数据" defaultImageType:DefaultImageTypeNoData];
        }else{
            [self.tableview removeDefaultView];
        }
        [SVProgressHUD dismiss];
        [self endRefresh];
    } failBlock:^(NSError *error) {
        [ShowMessage showHintMessage:@"请求失败!" duration:2.0 withRemindId:remindFail];
        [self endRefresh];
        [SVProgressHUD dismiss];

    }];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
